package com.andy.app_core.net;


import com.andy.app_core.net.callback.IError;
import com.andy.app_core.net.callback.IFailure;
import com.andy.app_core.net.callback.IRequest;
import com.andy.app_core.net.callback.ISuccess;
import com.andy.app_core.net.callback.RequestCallBacks;
import com.andy.app_core.net.download.DownloadHandler;

import java.io.File;
import java.util.HashMap;

import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;

/**
 * @version V1.0
 * @描述:
 * @Company:
 * @auth: Created by AN .
 * @time: 2020/3/26 16:08
 * @des
 */
public class RestClient {
    private final HashMap<String,Object> PARAMS;
    private final String URL;
    private final IRequest REQUEST;
    private final ISuccess SUCCESS;
    private final IFailure FAILURE;
    private final IError ERROR;
    private final RequestBody BODY;
    //上传下载
    private final File FILE;
    private final String DOWNLOAD_DIR;
    private final String EXTENSION;//扩展名
    private final String FILENAME;

    public RestClient(HashMap<String, Object> PARAMS,
                      String URL,
                      IRequest REQUEST,
                      ISuccess SUCCESS,
                      IFailure FAILURE,
                      IError ERROR,
                      RequestBody BODY,
                      File FILE,
                      String DOWNLOAD_DIR,
                      String EXTENSION,
                      String FILENAME) {
        this.PARAMS = PARAMS;
        this.URL = URL;
        this.REQUEST = REQUEST;
        this.SUCCESS = SUCCESS;
        this.FAILURE = FAILURE;
        this.ERROR = ERROR;
        this.BODY = BODY;
        this.FILE = FILE;
        this.DOWNLOAD_DIR = DOWNLOAD_DIR;
        this.EXTENSION = EXTENSION;
        this.FILENAME = FILENAME;
    }
    public static RestClientBuilder create(){
        return new RestClientBuilder();
    }
    /**
     * 执行网络请求操作
     */
    private void request(HttpMethod method){
        Call<String> call = null;
        RestService service = RestCreator.getRestService();
        if(REQUEST!=null){
            REQUEST.onRequestStart();
        }
        switch (method) {
            case GET:
                call = service.get(URL, PARAMS);
                break;
            case POST:
                call = service.post(URL, PARAMS);
                break;
            case POST_RAW:
                call = service.postRaw(URL,BODY);
                break;
            case PUT:
                call = service.put(URL, PARAMS);
                break;
            case PUT_RAW:
                call = service.putRaw(URL, BODY);
                break;
            case DELETE:
                call = service.delete(URL, PARAMS);
                break;
            case UPLOAD:
                final RequestBody requestBody = RequestBody.create(MultipartBody.FORM,FILE);
                final MultipartBody.Part body=MultipartBody.Part
                        .createFormData("file",FILE.getName(),requestBody);
                call=service.upload(URL,body);
                break;
        }
        if (call !=null){
            call.enqueue(new RequestCallBacks( REQUEST,  SUCCESS,  ERROR,  FAILURE));
        }
    }
    //外部請求調用
    public final void get(){
        request(HttpMethod.GET);
    }
    public final void post(){
        request(HttpMethod.POST);
    }
    public final void put(){
        request(HttpMethod.PUT);
    }
    public final void delete(){
        request(HttpMethod.DELETE);
    }
    public final void upload(){
        request(HttpMethod.UPLOAD);
    }
    public  final void download(){
        new DownloadHandler(PARAMS,URL,REQUEST,SUCCESS,FAILURE,ERROR,DOWNLOAD_DIR,EXTENSION,FILENAME)
                .handleDownload();
    }

}
